import QtQuick 2.15
import QtQuick.Layouts 1.14
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls 2.14
Item {
    id:root
    property alias length: input.text
    property string lineColor:"#F517B2"
    property string textColor : "#000"
    property bool readOnly:true

    signal sendData(var value)

    RowLayout{
         anchors.fill: parent
         spacing: 0
         Rectangle{
            height: 12
            width: 2
            color: root.lineColor
         }

         Rectangle{
            height: 2
            Layout.fillWidth: true
            color: root.lineColor
         }

         Rectangle{
             implicitWidth: input.contentWidth + 10
             implicitHeight: input.contentHeight + 4
             color: "transparent"
             border.color: input.focus?"#1989FA":"transparent"
             border.width:input.focus?2:0
             radius: 4
             Layout.leftMargin: 5
             Layout.rightMargin: 2
             Layout.alignment: Qt.AlignCenter | Qt.AlignHCenter

             TextInput{
                 id: input
                 anchors.fill: parent
                 text: ""
                 font{
                     family: "Microsoft YaHei"
                     pixelSize: 22
                 }
                 padding: 0
                 color: root.textColor
                 inputMethodHints: Qt.ImhDigitsOnly
                 horizontalAlignment: TextInput.AlignHCenter
                 verticalAlignment: TextInput.AlignVCenter
                 selectedTextColor:"#FFF"
                 selectionColor: "#4CA1F6"
                 readOnly: root.readOnly
                 focus:false

                 onEditingFinished:{
                     text = parseFloat(text) + ''
                     root.sendData(parseFloat(text))
                     input.focus = false
                 }

                 onFocusChanged: {
                     if(input.focus){
                         input.selectAll()
                         numberInput.toUp()
                     }else{
                         input.deselect()
                         numberInput.toDown()
                     }
                 }
             }
         }

         Connections{
             target: numberInput
             function onSendData(data){
                 if(input.focus){
                     input.text = data
                     input.editingFinished()
                 }
             }
         }

         Connections{
             target: printControl
             function onSizeUnitChanged(){
                 unitLabel.text = printControl.sizeUnit;
             }
         }

         Label{
            id:unitLabel
            text:printControl.getSizeInitUnit()
            font.pixelSize: 22
            color: root.textColor
            Layout.alignment: Qt.AlignCenter | Qt.AlignHCenter
            Layout.rightMargin: 10
         }

         Rectangle{
            height: 2
            Layout.fillWidth: true
            color: root.lineColor
         }

         Rectangle{
             height: 12
             width: 2
             color: root.lineColor
         }
     }
}
